var app = getApp();
var custId = app.globalData.custID;
var common = require("../../utils/Conmon.js");
Page({
  linkToMall: function () {

    wx.switchTab({
      url: '/pages/customIndex/index'
    })
  },
  /**
   * 页面的初始数据
   */
  data: {
    popin: false,
    list: [],
    selectProductId: 0,
    selectShopID:0,
    nodata:true
  },
  changePopin: function (e) {
    var pid = e.currentTarget.id;
    var selectShopID = e.currentTarget.dataset.shopid;
    this.setData({
      popin: !this.data.popin,
      selectProductId: pid,
      selectShopID: selectShopID
    })
  },
  gotoproduct: function () {
    this.setData({
      popin: false,
    })
    var pid = this.data.selectProductId;
    var selectShopID = this.data.selectShopID;
    wx.navigateTo({
      url: '/pages/details/details?productId=' + pid + "&shopID=" + selectShopID
    })
  },
  //清空收藏
  delAll: function (e) {
    wx.showModal({
      title: '提示',
      content: '是否清空所有收藏？',
      showCancel: true,
      confirmText: "确定",
      success: function (res) {
        if (res.confirm) {
          wx.showToast({
            title: '加载中...',
            icon: 'loading'
          })
          wx.request({
            url: app.globalData.apiurl + '/mobile/api/Collection/ClearMemberCollection',
            data: { custId: custId, userId: wx.getStorageSync('UserInfo').UserID },
            method: 'GET',
            header: {
              'X_ManuName': app.globalData.manuName
            },
            success: function (res) {
              wx.hideToast();
              if (res.data.code == 1200) {
                var page = getCurrentPages().pop()
                if (page == undefined || page == null)
                  return;
                page.onLoad()
              } else {
                wx.showModal({
                  content: res.data.msg,
                  showCancel: false,
                  confirmText: "确定",
                })
              }
            }
          })
        }
      }
    })
  },
  //删除
  del: function (e) {
    var that = this;
    that.setData({
      popin: false,
    })
    wx.showModal({
      title: '提示',
      content: '是否删除收藏？',
      showCancel: true,
      confirmText: "确定",
      success: function (res) {
        if (res.confirm) {
          wx.showToast({
            title: '加载中...',
            icon: 'loading'
          })
          var data =
            {
              CustId: custId,
              UserId: wx.getStorageSync('UserInfo').UserID,
              ProductID: that.data.selectProductId,
              UnionShopID: that.data.selectShopID
            };
          wx.request({
            url: app.globalData.apiurl + '/mobile/api/Collection/CancelCollection',
            data: JSON.stringify(data),
            header: {
              'X_ManuName': app.globalData.manuName
            },
            method: 'POST',
            success: function (res) {
              wx.hideToast();
              if (res.data.code == 1200) {
                that.setData({
                  selectProductId: 0
                })
                var page = getCurrentPages().pop()
                if (page == undefined || page == null)
                  return;
                page.onLoad()
              } else {
                wx.showModal({
                  content: res.data.msg,
                  showCancel: false,
                  confirmText: "确定",
                })
              }
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    common.pageInit( function () { });
    loadCollect(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

//加载数据
function loadCollect(that) {
  wx.showToast({
    title: '加载中...',
    icon: 'loading'
  })
  var data =
    {
      CustId: custId,
      UserId: wx.getStorageSync('UserInfo').UserID,
      PageIndex: 1,
      PageSize: 10
    };
  wx.request({
    url: app.globalData.apiurl + '/mobile/api/Collection/GetUnionMemberCollectionPage',
    data: JSON.stringify(data),
    method: 'POST',
    header: {
      'X_ManuName': app.globalData.manuName
    },
    success: function (res) {
      wx.hideToast();
      if (that.data.PageIndex = 1 && res.data.content.length == 0) {
        that.setData({ nodata: false });
      } else {
        that.setData({ nodata: true });
      }   
      if (res.data.code == 1200) {
        that.setData({
          list: res.data.content
        });
      } else {
        console.log('加载失败');
      }
    }
  })
}